using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using WorkflowDataTypes = Scenario.WorkflowEngine.DataModel.WorkflowDataTypes;


namespace Scenario.Modeler
{
	/// <summary>
	/// Summary description for FormDataType.
	/// </summary>
	public class FormDataType : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label Key;
		private System.Windows.Forms.Label Type;
		private System.Windows.Forms.Label Description;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txtKey;
		private System.Windows.Forms.ComboBox cmbType;
		private System.Windows.Forms.TextBox txtDescription;
		private System.Windows.Forms.ComboBox cmbEnumType;
		private System.Windows.Forms.ListView lstValues;
		private System.Windows.Forms.TextBox txtValKey;
		private System.Windows.Forms.TextBox txtValValue;
		private System.Windows.Forms.Button btnValAdd;
		private System.Windows.Forms.Button btnValModify;
		private System.Windows.Forms.Button btnValDelete;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnXML;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;


		public WorkflowDataTypes.XMLDataType xmlDataType = null;

		public FormDataType()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			this.xmlDataType = new WorkflowDataTypes.XMLDataType();
		}

		public FormDataType(WorkflowDataTypes.XMLDataType xmlDT) : this()
		{
			this.xmlDataType = xmlDT;

			this.ShowDataType();
		}

		private void ShowDataType()
		{
			this.txtKey.Text = this.xmlDataType.Key;
			this.txtDescription.Text = this.xmlDataType.Description;
			this.cmbType.Text = this.xmlDataType.Type;

			this.cmbEnumType.Text = this.xmlDataType.XMLEnumerationValues.Type;

			for ( int i=0; i<this.xmlDataType.XMLEnumerationValues.Count; i++)
			{
				WorkflowDataTypes.XMLDataTypeValue xmlValue = (WorkflowDataTypes.XMLDataTypeValue)this.xmlDataType.XMLEnumerationValues[i];

				this.lstValues.Items.Add (new ListViewItem( new string[] { xmlValue.Key, xmlValue.Value }) );
			}

			this.lstValues.Refresh();
		}

		private void MakeObject()
		{
			this.xmlDataType.Key = this.txtKey.Text;
			this.xmlDataType.Description = this.txtDescription.Text;
			this.xmlDataType.Type = this.cmbType.Text;

			this.xmlDataType.XMLEnumerationValues.Type = this.cmbEnumType.Text;
			this.xmlDataType.XMLEnumerationValues.Clear();

			for ( int i=0; i<this.lstValues.Items.Count; i++)
			{
				ListViewItem item = this.lstValues.Items[i];

				WorkflowDataTypes.XMLDataTypeValue xmlValue = new WorkflowDataTypes.XMLDataTypeValue();
				
				xmlValue.Key = item.Text;
				xmlValue.Value = item.SubItems[0].Text;

				this.xmlDataType.XMLEnumerationValues.Add (xmlValue);
			}
		}


		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.Key = new System.Windows.Forms.Label();
            this.Type = new System.Windows.Forms.Label();
            this.Description = new System.Windows.Forms.Label();
            this.txtKey = new System.Windows.Forms.TextBox();
            this.cmbType = new System.Windows.Forms.ComboBox();
            this.txtDescription = new System.Windows.Forms.TextBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.btnValDelete = new System.Windows.Forms.Button();
            this.btnValModify = new System.Windows.Forms.Button();
            this.btnValAdd = new System.Windows.Forms.Button();
            this.txtValValue = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.txtValKey = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.lstValues = new System.Windows.Forms.ListView();
            this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
            this.cmbEnumType = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.panel2 = new System.Windows.Forms.Panel();
            this.btnXML = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // Key
            // 
            this.Key.Location = new System.Drawing.Point(7, 14);
            this.Key.Name = "Key";
            this.Key.Size = new System.Drawing.Size(126, 14);
            this.Key.TabIndex = 0;
            this.Key.Text = "Key";
            // 
            // Type
            // 
            this.Type.Location = new System.Drawing.Point(200, 7);
            this.Type.Name = "Type";
            this.Type.Size = new System.Drawing.Size(100, 14);
            this.Type.TabIndex = 1;
            this.Type.Text = "Type";
            // 
            // Description
            // 
            this.Description.Location = new System.Drawing.Point(7, 62);
            this.Description.Name = "Description";
            this.Description.Size = new System.Drawing.Size(83, 20);
            this.Description.TabIndex = 2;
            this.Description.Text = "Description";
            // 
            // txtKey
            // 
            this.txtKey.Location = new System.Drawing.Point(7, 28);
            this.txtKey.Name = "txtKey";
            this.txtKey.Size = new System.Drawing.Size(180, 20);
            this.txtKey.TabIndex = 3;
            // 
            // cmbType
            // 
            this.cmbType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbType.Items.AddRange(new object[] {
            "STRING",
            "NUMBER",
            "XML"});
            this.cmbType.Location = new System.Drawing.Point(200, 28);
            this.cmbType.Name = "cmbType";
            this.cmbType.Size = new System.Drawing.Size(101, 21);
            this.cmbType.TabIndex = 4;
            // 
            // txtDescription
            // 
            this.txtDescription.Location = new System.Drawing.Point(7, 76);
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.Size = new System.Drawing.Size(293, 20);
            this.txtDescription.TabIndex = 5;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.btnValDelete);
            this.groupBox1.Controls.Add(this.btnValModify);
            this.groupBox1.Controls.Add(this.btnValAdd);
            this.groupBox1.Controls.Add(this.txtValValue);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.txtValKey);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.lstValues);
            this.groupBox1.Controls.Add(this.cmbEnumType);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.groupBox1.Location = new System.Drawing.Point(7, 111);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(293, 395);
            this.groupBox1.TabIndex = 7;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Enumeration";
            // 
            // btnValDelete
            // 
            this.btnValDelete.Location = new System.Drawing.Point(213, 361);
            this.btnValDelete.Name = "btnValDelete";
            this.btnValDelete.Size = new System.Drawing.Size(63, 19);
            this.btnValDelete.TabIndex = 9;
            this.btnValDelete.Text = "Delete";
            this.btnValDelete.Click += new System.EventHandler(this.btnValDelete_Click);
            // 
            // btnValModify
            // 
            this.btnValModify.Location = new System.Drawing.Point(213, 326);
            this.btnValModify.Name = "btnValModify";
            this.btnValModify.Size = new System.Drawing.Size(63, 20);
            this.btnValModify.TabIndex = 8;
            this.btnValModify.Text = "Modify";
            this.btnValModify.Click += new System.EventHandler(this.btnValModify_Click);
            // 
            // btnValAdd
            // 
            this.btnValAdd.Location = new System.Drawing.Point(213, 291);
            this.btnValAdd.Name = "btnValAdd";
            this.btnValAdd.Size = new System.Drawing.Size(63, 20);
            this.btnValAdd.TabIndex = 7;
            this.btnValAdd.Text = "Add";
            this.btnValAdd.Click += new System.EventHandler(this.btnValAdd_Click);
            // 
            // txtValValue
            // 
            this.txtValValue.Location = new System.Drawing.Point(13, 361);
            this.txtValValue.Name = "txtValValue";
            this.txtValValue.Size = new System.Drawing.Size(140, 20);
            this.txtValValue.TabIndex = 6;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(13, 347);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(67, 14);
            this.label3.TabIndex = 5;
            this.label3.Text = "Value";
            // 
            // txtValKey
            // 
            this.txtValKey.Location = new System.Drawing.Point(13, 312);
            this.txtValKey.Name = "txtValKey";
            this.txtValKey.Size = new System.Drawing.Size(140, 20);
            this.txtValKey.TabIndex = 4;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(13, 291);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(74, 14);
            this.label2.TabIndex = 3;
            this.label2.Text = "Key";
            // 
            // lstValues
            // 
            this.lstValues.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader2});
            this.lstValues.FullRowSelect = true;
            this.lstValues.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
            this.lstValues.Location = new System.Drawing.Point(7, 69);
            this.lstValues.Name = "lstValues";
            this.lstValues.Size = new System.Drawing.Size(273, 208);
            this.lstValues.TabIndex = 2;
            this.lstValues.UseCompatibleStateImageBehavior = false;
            this.lstValues.View = System.Windows.Forms.View.Details;
            this.lstValues.ItemActivate += new System.EventHandler(this.lstValues_ItemActivate);
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "Key";
            this.columnHeader1.Width = 137;
            // 
            // columnHeader2
            // 
            this.columnHeader2.Text = "Value";
            this.columnHeader2.Width = 178;
            // 
            // cmbEnumType
            // 
            this.cmbEnumType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbEnumType.Items.AddRange(new object[] {
            "Enumeration",
            "Range"});
            this.cmbEnumType.Location = new System.Drawing.Point(7, 35);
            this.cmbEnumType.Name = "cmbEnumType";
            this.cmbEnumType.Size = new System.Drawing.Size(140, 21);
            this.cmbEnumType.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(7, 21);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(83, 20);
            this.label1.TabIndex = 0;
            this.label1.Text = "Type";
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.btnXML);
            this.panel2.Controls.Add(this.btnSave);
            this.panel2.Controls.Add(this.btnCancel);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 647);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(368, 41);
            this.panel2.TabIndex = 16;
            // 
            // btnXML
            // 
            this.btnXML.Location = new System.Drawing.Point(227, 7);
            this.btnXML.Name = "btnXML";
            this.btnXML.Size = new System.Drawing.Size(60, 28);
            this.btnXML.TabIndex = 11;
            this.btnXML.Text = "XML";
            this.btnXML.Click += new System.EventHandler(this.btnXML_Click);
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(93, 7);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(67, 28);
            this.btnSave.TabIndex = 9;
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(13, 7);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(67, 28);
            this.btnCancel.TabIndex = 10;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // FormDataType
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(368, 688);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.txtDescription);
            this.Controls.Add(this.cmbType);
            this.Controls.Add(this.txtKey);
            this.Controls.Add(this.Description);
            this.Controls.Add(this.Type);
            this.Controls.Add(this.Key);
            this.Name = "FormDataType";
            this.Text = "FormDataType";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.panel2.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		

		private void btnXML_Click(object sender, System.EventArgs e)
		{
			this.MakeObject();

			FormXML frmXML = new FormXML();
			frmXML.XML = this.xmlDataType.GetXML (true,1);
			frmXML.ShowDialog(this);

			if ( frmXML.OK )
			{
				this.xmlDataType.Parse ( frmXML.XML);
				this.ShowDataType();
			}
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.xmlDataType = null;

			this.Close();
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			this.MakeObject();

			this.Close();
		}

		private void btnValAdd_Click(object sender, System.EventArgs e)
		{
			if ( ( this.txtValKey.Text.Length > 0 ) ||  ( this.txtValValue.Text.Length > 0 ) )
			{
				ListViewItem item = new ListViewItem( new string[] { txtValKey.Text, this.txtValValue.Text }) ;

				this.lstValues.Items.Add (item);
				this.lstValues.Refresh();

				this.txtValKey.Text = string.Empty;
				this.txtValValue.Text = string.Empty;

			}
			else
			{
				MessageBox.Show ("Key and Value are mandatory");
			}
		}

		private void btnValModify_Click(object sender, System.EventArgs e)
		{
			if ( this.lstValues.SelectedItems.Count != 1 )
			{
				MessageBox.Show ("It's mandatory select an item.");
				return;
			}

			this.lstValues.SelectedItems[0].Text = this.txtValKey.Text;
			this.lstValues.SelectedItems[0].SubItems[0].Text = this.txtValValue.Text;

			this.txtValKey.Text = string.Empty;
			this.txtValValue.Text = string.Empty;

			this.lstValues.Refresh();
		}

		private void btnValDelete_Click(object sender, System.EventArgs e)
		{
		
			if ( this.lstValues.SelectedItems.Count < 1 )
			{
				MessageBox.Show ("It's mandatory select at least one an item.");
				return;
			}

			for ( int i=0; i<this.lstValues.SelectedItems.Count;i++)
			{
				this.lstValues.Items.Remove(this.lstValues.SelectedItems[i]);
			}

			this.lstValues.Refresh();
		}

		private void lstValues_ItemActivate(object sender, System.EventArgs e)
		{
			this.txtValKey.Text = this.lstValues.SelectedItems[0].Text;
			this.txtValValue.Text = this.lstValues.SelectedItems[0].SubItems[0].Text;
		}


	}
}
